<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>加载glTF格式的文件</title>
    <style>
        body {
            margin: 0;
            overflow: hidden; /* 溢出隐藏 */
        }
    </style>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/build/three-r93.js"></script>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/Detector.js"></script>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/libs/dat.gui.min.js"></script>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/libs/stats.min.js"></script>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/controls/OrbitControls.js"></script>
    <script src="https://ithanmang.gitee.io/threejs/home/libs/examples/js/loaders/GLTFLoader.js"></script>
    <style>
        #loading {
            position: fixed;
            top: 50%;
            left: 50%;
            color: #FFFFFF;
            font-size: 20px;
            margin-top: -30px;
            margin-left: -40px;
        }
    </style>
</head>
<body>
<p id="loading">loading......</p>
<script>

    let threejsScene, threejsCamera, renderer, controls, guiControls;
    let stats = initStats();

    /* 场景 */
    function initScene() {

        threejsScene = new THREE.Scene();

    }

    /* 相机 */
    function initCamera() {

        threejsCamera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 10000);
        threejsCamera.position.set(5, 0, 10);
        threejsCamera.lookAt(new THREE.Vector3(0, 0, 0));

    }

    /* 渲染器 */
    function initRender() {

        renderer = new THREE.WebGLRenderer({antialias: true});
        renderer.setSize(window.innerWidth, window.innerHeight);
        renderer.setClearColor(0x0E3866);
        document.body.appendChild(renderer.domElement);

    }

    /* 灯光 */
    function initLight() {

        threejsScene.add(new THREE.AmbientLight(0xffffff));

    }

    /* 控制器 */
    function initControls() {

        controls = new THREE.OrbitControls(threejsCamera, renderer.domElement);

        /* 属性参数默认 */

    }

    /* 调试插件 */
    function initGui() {

        guiControls = new function () {

        };

        let controls = new dat.GUI({width: 200});

    }

    /* 场景中的内容 */
    function initContent() {

        // 加载 glTF 格式的模型
        let loader = new THREE.GLTFLoader();/*实例化加载器*/

        loader.load('./elphant/index.gltf',function (obj) {

            console.log(obj);
            
            obj.scene.position.y = 1;
            threejsScene.add(obj.scene);
            document.getElementById('loading').style.display = 'none';

        },function (xhr) {

            console.log( ( xhr.loaded / xhr.total * 100 ) + '% loaded' );

        },function (error) {

            console.log('load error!'+error.getWebGLErrorMessage());

        })

    }

    /* 性能插件 */
    function initStats() {

        let stats = new Stats();

        document.body.appendChild(stats.domElement);

        return stats;

    }

    /* 窗口变动触发 */
    function onWindowResize() {

        threejsCamera.aspect = window.innerWidth / window.innerHeight;
        threejsCamera.updateProjectionMatrix();
        renderer.setSize(window.innerWidth, window.innerHeight);

    }

    /* 数据更新 */
    function update() {

        stats.update();

    }

    /* 初始化 */
    function init() {

        initScene();
        initCamera();
        initRender();
        initLight();
        initControls();
        initContent();
        initGui();

        /* 监听事件 */
        window.addEventListener('resize', onWindowResize, false);

    }

    /* 循环渲染 */
    function threejsAnimate() {

        requestAnimationFrame(threejsAnimate);
        renderer.render(threejsScene, threejsCamera);
        update();

    }

    /* 初始加载 */
    (function () {
        console.log("three init start...");

        init();
        threejsAnimate();

        console.log("three init send...");
    })();

</script>
</body>
</html>
